package com.canic.dkd.web.shop.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.canic.dkd.base.entity.Result;
import com.canic.dkd.web.merchandise.dto.MerchandiseOutputDTO;
import com.canic.dkd.web.merchandise.service.MerchandiseService;
import com.canic.dkd.web.merchandise.service.MerchandiseTypeService;
import com.canic.dkd.web.merchandise.service.TypeService;
import com.canic.dkd.web.shop.dto.ShopOutputDTO;
import com.canic.dkd.web.shop.entity.Shop;
import com.canic.dkd.web.shop.service.ShopService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * ************实体关系说明***********
 * shop 实体： 店铺
 * merchandise 实体：商品  外键shop_id是店铺的id
 * type 实体：商品类型
 * merchandise_type 商品类型关系:商品N  VS  类型N   外键:type_id是商品类型id,merchandise_id是商品id
 *
 * ↓↓↓↓↓↓↓↓↓↓↓ CRUD demo ↓↓↓↓↓↓↓↓↓↓↓
 */

/**
 * shopContrller
 * @author ZC
 */
@Slf4j
@RestController
@RequestMapping("/shop")
public class ShopController {

    @Autowired private ShopService shopService;

    @Autowired private MerchandiseService merchandiseService;

    @Autowired private MerchandiseTypeService merchandiseTypeService;

    @Autowired private TypeService typeService;


    /**
     * 创建对象：单体对象
     * (创建时间，创建人，通过 MybatisAuditAOPDefault 定义的切面统一设置)
     * @param shop
     * @return
     */
    @RequestMapping(value= "/save")
    public Result save(@RequestBody Shop shop){
        shop.setId(UUID.randomUUID().toString());
        try{
            shopService.insert(shop);
            return Result.successResult(shop);
        }catch (Exception e){
            log.error(e.getMessage());
            return Result.failureResult(e);
        }
    }

    /**
     * 更新对象:单体对象
     * (更新时间，修改人，通过 MybatisAuditAOPDefault 定义的切面统一设置)
     * @param shop
     * @return
     */
    @RequestMapping(value= "/update")
    public Result update(@RequestBody Shop shop){
        try{
            shopService.updateById(shop);
            Shop updateShop = shopService.selectById(shop.getId());
            return Result.successResult(updateShop);
        }catch (Exception e){
            log.error(e.getMessage());
            return Result.failureResult(e);
        }
    }

    /**
     * xml查询全部(单表):不带查询条件
     * @return
     */
    @RequestMapping("/fullList")
    public Result fullList(){
        try{
            List<Shop> list = shopService.fullList();
            return Result.successResult(list);
        }catch (Exception e){
            log.error(e.getMessage());
            return Result.failureResult(e);
        }
    }

    /**
     * xml分页查询(单表):不带查询条件
     * @param pageNumber
     * @param pageSize
     * @return
     */
    @RequestMapping("fullPageList")
    public Result fullPageList(@RequestParam(value = "pageNumber",required = false,defaultValue = "1") Integer pageNumber,
                               @RequestParam(value = "pageSize",required = false,defaultValue = "10") Integer pageSize){
        try{
            Page page = new Page<>(pageNumber,pageSize);
            Page<Shop> pageList = shopService.findAllShopPage(page);
            return Result.successResult(pageList);
        }catch (Exception e){
            log.error(e.getMessage());
            return Result.failureResult(e);
        }
    }

    /**
     * xml查询(单表):带查询条件(查询条件不固定)
     * @param shop
     * @return
     */
    @RequestMapping("eligibleList")
    public Result eligibleList(@RequestBody Shop shop){
        try{
            List<Shop> list = shopService.eligibleList(shop);
            return Result.successResult(list);
        }catch (Exception e){
            log.error(e.getMessage());
            return Result.failureResult(e);
        }
    }

    /**
     * xml分页查询(单表):带查询条件(查询条件不固定)
     * @param pageNumber
     * @param pageSize
     * @param shop
     * @return
     */
    @RequestMapping("eligiblePageList")
    public Result eligiblePageList(@RequestParam(value = "pageNumber",required = false,defaultValue = "1") Integer pageNumber,
                                   @RequestParam(value = "pageSize",required = false,defaultValue = "10") Integer pageSize,
                                   @RequestBody Shop shop){
        try{
            Page page = new Page<>(pageNumber,pageSize);
            Page<Shop> pageList = shopService.eligiblePageList(page, shop);
            return Result.successResult(pageList);
        }catch (Exception e){
            log.error(e.getMessage());
            return Result.failureResult(e);
        }
    }

    /**
     * xml分页查询(多表):带查询条件(查询条件不固定) 查询店铺所有信息商品按分类展示(三层头体结构只查到类别类别下商品没查到，所以是两层头体结构)
     * @param pageNumber
     * @param pageSize
     * @return
     */
    @RequestMapping("eligibleComplexPageList")
    public Result eligibleComplexPageList(@RequestParam(value = "pageNumber",required = false,defaultValue = "1") Integer pageNumber,
                                   @RequestParam(value = "pageSize",required = false,defaultValue = "10") Integer pageSize,
                                   @RequestBody Map<String, Object> paraMap){
        try{
            Page page = new Page<>(pageNumber,pageSize);
            Page<ShopOutputDTO> pageList = shopService.eligibleComplexPageList(page, paraMap);
            return Result.successResult(pageList);
        }catch (Exception e){
            log.error(e.getMessage());
            return Result.failureResult(e);
        }
    }

    /**
     * xml分页查询(多表):带查询条件(查询条件不固定)
     * @param pageNumber
     * @param pageSize
     * @return
     */
    @RequestMapping("getMerchandiseListByTypeId")
    public Result getMerchandiseListByTypeId(@RequestParam(value = "pageNumber",required = false,defaultValue = "1") Integer pageNumber,
                                             @RequestParam(value = "pageSize",required = false,defaultValue = "10") Integer pageSize,
                                             @RequestBody Map<String, Object> paraMap){
        try{
            Page page = new Page<>(pageNumber,pageSize);
            Page<MerchandiseOutputDTO> pageList = shopService.getMerchandiseListByTypeId(page, paraMap);
            return Result.successResult(pageList);
        }catch (Exception e){
            log.error(e.getMessage());
            return Result.failureResult(e);
        }
    }


}
